<?php
class TypesController extends AppController{

     var $name='Types';
     var $uses = array("Codetype","Codevalue","Constant","Techstack","Talstack");     
     var $helpers=array('Pagination','Html','Javascript','Form','Fck','Str','Action','Accp');
     var $components = array ('Pagination');
     var $layout='hindex';
     
    function index(){
        $this->redirect('/users/index');
        exit(); 
    }
    function codetypes()
    {
        $where="";
        $where="Codetype.type not in ('项目所处阶段','注册','内资注册','外资注册')";
        $codetypes = $this->Codetype->findAll($where);
//		debug($codetypes);
        $this->set('codetypes',$codetypes);
    }
    function editcodevalue($id,$codevalue)
    {
        $this->Codevalue->id=$id;
        $isedit=$this->Codevalue->saveField('value', urldecode($codevalue));
        exit();
    }
    function editcodetype($id,$codetype)
    {
        $this->Codetype->id=$id;
        $this->Codetype->saveField('type',urldecode($codetype));
        exit();
    }
    function delcodevalue($id)
    {
        $this->Codevalue->del($id);
        exit();
    }
    function delcodetype($id)
    {
        $this->Codetype->del($id);
        exit();
    }
    function getlastsort($typeid)
    {
        $objcodevalue=$this->Codevalue->findAll("codetype_id='$typeid'",null,"sort desc",1,null);
        $sort=$objcodevalue[0]["Codevalue"]["sort"];
        return $sort+1;
    }
    function addcodevalue($typeid,$cvalue)
    {
        $objcvlue=array();
        $objcvlue["Codevalue"]["codetype_id"]=$typeid;
        $objcvlue["Codevalue"]["value"]=urldecode($cvalue);
        $sort=$this->getlastsort($typeid);
        $objcvlue["Codevalue"]["sort"]=$sort;
        $this->Codevalue->save($objcvlue);
        $cvid=$this->Codevalue->getLastInsertId();
        $dan='"';
        print("<input id='txt".$cvid."' type='text' 
        value='".$cvalue."'  class='txtreadonly'
        onclick='edittxt(this)' onkeypress='TextValidate()' maxlength='20'
        onblur='changetxt(this,".$dan.$cvid.$dan.",".$dan.urldecode($cvalue).$dan.")'
        />
        <span class='code_delete' onclick='delcvalue(this,".$dan.$cvid.$dan.")'></span>
        <span class='code_up' onclick='up(this,".$dan.$typeid.$dan.",".$dan.$cvid.$dan.")'></span>
        <span class='code_down' onclick='down(this,".$dan.$typeid.$dan.",".$dan.$cvid.$dan.")'></span>
        <div class='clear'></div>
        ");
        exit();
    }
    
    function addcodetypes()
    {
        if(!empty($this->data))
        {
            $this->Codetype->saveAll($this->data);
            $this->Session->setFlash('类型保存成功');
            $this->redirect(array('action'=>'codetypes'));
        }
    }
    function getsort($id){
        $sort=0;
        $objcodevalue=$this->Codevalue->findById($id);
        $sort=$objcodevalue["Codevalue"]["sort"];
        return $sort;
    }
    function up($typeid,$id){
        $sort=$this->getsort($id);
        $neighbors = $this->Codevalue->find('neighbors',
        array('field' => 'sort', 'value' => $sort
        ,'order'=>array('Codevalue.sort'=>'asc')
        ,'conditions'=>array("codetype_id='$typeid'")));
        
        $previd=$neighbors["prev"]["Codevalue"]["id"];
        $prevsort=$neighbors["prev"]["Codevalue"]["sort"];
        
        $this->Codevalue->id=$id;
        $this->Codevalue->saveField('sort',$prevsort);
        $this->Codevalue->id=$previd;
        $this->Codevalue->saveField('sort',$sort);
        exit();
    }
    function down($typeid,$id){
        $sort=$this->getsort($id);
        $neighbors = $this->Codevalue->find('neighbors',
        array('field' => 'sort', 'value' => $sort
        ,'order'=>array('Codevalue.sort'=>'asc')
        ,'conditions'=>array("codetype_id='$typeid'")));
        $nextid=$neighbors["next"]["Codevalue"]["id"];
        $nextsort=$neighbors["next"]["Codevalue"]["sort"];
        $this->Codevalue->id=$id;
        $this->Codevalue->saveField('sort',$nextsort);
        $this->Codevalue->id=$nextid;
        $this->Codevalue->saveField('sort',$sort);
        exit();
    }
    //人才项目申报
    var $tree="";
    function talstack(){
        $this->showtrees();
        $this->set('tree',$this->tree);
    }
    function deltalstack($id){
        $this->Talstack->id=$id;
        $this->Talstack->saveField("is_deleted",'1');
        $objtal=$this->Talstack->findAllBySuperClass($id);
        if($objtal){
            foreach ($objtal as $tal){
                $talid=$tal["Talstack"]["id"];
                $this->Talstack->id=$talid;
                $this->Talstack->saveField("is_deleted",'1');
                $objsubtal=$this->Talstack->findAllBySuperClass($talid);
                if($objsubtal){
                    foreach ($objsubtal as $subtal){
                        $subtalid=$subtal["Talstack"]["id"];
                        $this->Talstack->id=$subtalid;
                        $this->Talstack->saveField("is_deleted",'1');
                    }
                }
            }
        }
    }
    function showtrees()
    {
        $user=$this->Session->read('User');
        $iadmin=$user['isadmin'];
        $isgroupadmin=$user['isgroupadmin'];
        $isadmin=false;
        if($iadmin || $isgroupadmin){
            $isadmin=true;
        }
        $where="(Talstack.super_class is null and Talstack.is_deleted='0')";
        $talstacks = $this->Talstack->findAll($where,null,"Talstack.created asc");
        $dan="'";
        if($talstacks){
            $this->tree="<ul>";
            App::import('Helper','Html');
            $Html=new HtmlHelper();
            foreach($talstacks as $talstack){
                
                $tname=$talstack["Talstack"]["tname"];
                $tid=$talstack["Talstack"]["id"];
                $superclass=$talstack["Talstack"]["super_class"];
                $typename=array("tname"=>$tname,"tid"=>$tid,"superclass"=>$superclass);
                $this->tree.="<li>";
                if($isadmin){
                	$this->tree.="<span class='talstack_title_admin'>".$Html->link($tname,"/types/showtalstack/".$tid)."</span>";
                    $this->tree.="<span class='talstack_edit'>".$Html->link("","/types/edittalstack/".$tid)."</span>";
                    $this->tree.='<span class="talstack_del"><a onclick="deltype(this,'.$dan.$tid.$dan.')"></a></span>';
                }else{
                	$this->tree.="<span class='talstack_title'>".$Html->link($tname,"/types/showtalstack/".$tid)."</span>";
                }
                $this->recurstree($tid);
                $this->tree.="<div class='clear'></div></li>";
            }
            $this->tree.="</ul>";
        }
    }
    function recurstree($id)
    {
        $dan="'";
        $where="";
        $where.="(Talstack.super_class='$id' and Talstack.is_deleted='0')";
        $talstacks=$this->Talstack->findAll($where);
        if($talstacks){
            $this->tree.="<ul>";
            foreach($talstacks as $talstack){
                $tname=$talstack["Talstack"]["tname"];
                $tid=$talstack["Talstack"]["id"];
                $superclass=$talstack["Talstack"]["super_class"];
                $this->tree.="<li>";
                $this->tree.=$tname;
                    $this->recurstree($tid);
                $this->tree.="</li>";
            }
            $this->tree.="</ul>";
        }
    }
    function edittalstack($id=null)
    {			
        $objsubtype=array();
        $objsubtypes=array();
        if($id){
            $this->data=$this->Talstack->findById($id);
            $this->Talstack->unbindModel(array ('belongsTo' => array ('Codevalue')));
            $objsubtype=$this->Talstack->findAll("super_class='$id' and is_deleted='0'","id,tname");
            $sub=0;
            foreach ($objsubtype as $subtype){
                $subtypeid=$subtype["Talstack"]["id"];
                $this->Talstack->unbindModel(array ('belongsTo' => array ('Codevalue')));
                $subtype=$this->Talstack->findAll("super_class='$subtypeid' and is_deleted='0'","id,tname");
                $objsubtypes[]=$subtype;
                $sub++;
            }
        }else{
            if(!empty($this->data))
            {		
                App::import('Helper','Action');
                $actionHelper=new ActionHelper();
                $uuid="";
                $uid=$this->data["Talstack"]["id"];
                if($uid){
                    $uuid=$uid;
                }else{
                    $uuid=$actionHelper->uuid();	
                }           	
                $this->data["Talstack"]["id"]=$uuid;	           	
                $desc=$this->data["Talstack"]["desc"];
                $gov=$this->data["Talstack"]["gov"];
                $this->Talstack->save($this->data["Talstack"]);	   
                for($i=0;$i<3;$i++){       
                    if(!empty($this->data["Talstack"][$i]["tname"])){
                        $tid="";
                        $subtid=$this->data["Talstack"][$i]["id"];
                        if($subtid){
                            $tid=$subtid;
                        }else{
                            $tid=$actionHelper->uuid();	
                        } 
                        $this->data["Talstack"][$i]["id"]=$tid;
                        $this->data["Talstack"][$i]["super_class"]=$uuid;
                        $this->data["Talstack"][$i]["desc"]=$desc;
                        $this->data["Talstack"][$i]["gov"]=$gov;
                        $this->Talstack->save($this->data["Talstack"][$i]);	        		
                        $objtypelist=array();
                        $objtalsuper_class=$tid;
                        $objtalgov=$gov;
                        $objtaldesc=$desc;	
                        if(!empty($_POST['subtype'.$i])){				
                            $j=0;				
                            foreach ($_POST['subtype'.$i] as $type)
                            {											
                                if(trim($type)!=""){
                                    $objlist=array("tname"=>$type,"super_class"=>$objtalsuper_class,"gov"=>$objtalgov,"desc"=>$objtaldesc);
                                    if($_POST['subtypeid'.$i][$j]!=""){
                                        $objlist["id"]=$_POST['subtypeid'.$i][$j];
                                    }
                                    $objtypelist[]=$objlist;
                                }else{
                                    if($_POST['subtypeid'.$i][$j]!=""){
                                        $objdellist["id"]=$_POST['subtypeid'.$i][$j];
                                        $objdellist["is_deleted"]="1";
                                        $objtypelist[]=$objdellist;
                                    }
                                }
                                $j++;												
                            }
                            if($objtypelist){
                                $this->Talstack->saveAll($objtypelist);	
                            }	
                        }	
                    }else{
                        if($this->data["Talstack"][$i]["id"]!=""){
                            $objdelsubtype["id"]=$this->data["Talstack"][$i]["id"];
                            $objdelsubtype["is_deleted"]="1";
                            $this->Talstack->save($objdelsubtype);	
                        }
                    }
                }
               $this->redirect(array('action'=>'talstack'));
            }
        }
        $talstacktype=$this->Codetype->getSubType($this->Constant->TalstackType);		
        $this->set(compact('talstacktype','objsubtype','objsubtypes','id'));
    }
    
    function showtalstack($id){				
        $talstack=$this->Talstack->findById($id);
        $where="";
        $where.="(Talstack.super_class='$id' and Talstack.is_deleted='0')";
        $stacks = $this->Talstack->findAll($where);
        $talstacktype=$this->Codetype->getSubType($this->Constant->TalstackType,"-",1);		
        $this->set(compact('talstack','stacks','talstacktype'));
            
    }
    function checktalname($talname)
    {
        $isflag=0;
        $objname=array();
        $this->Talstack->unbindModel(array ('belongsTo' => array ('Codevalue')));
        $objname=$this->Talstack->findCount("Talstack.tname='".urldecode($talname)."' and super_class is null and is_deleted='0'");
        if($objname>0){
            $isflag=1;
        }
        print($isflag);
        exit();
    }
    //打开页面前执行的方法
    function beforeFilter()
    {
        parent::beforeFilter();
        $user=$this->Session->read('User');
        if (!$user){
            $this->redirect('/users/login');
        }
    }
    //打开页面后执行的方法
    function afterFilter()
    {
        
    }
}
?>
